<!--  -->
<template>
    <div class>
        <div class="imgs-box">
            <div class="img-box">
                <div class="title">货物图片1：</div>
                <el-image style="width: 250px; height: 250px" :src="formDialogTableData.hpic" :fit="'cover'"
                    :preview-src-list="[formDialogTableData.hpic]">
                    <div slot="error" class="image-slot">
                        暂无图片
                    </div>
                </el-image>
            </div>
            <div class="img-box">
                <div class="title">二维码：</div>
                <el-image style="width: 250px; height: 250px" :src="formDialogTableData.hpic2" :fit="'cover'"
                    :preview-src-list="[formDialogTableData.hpic2]">
                    <div slot="error" class="image-slot">
                        暂无图片
                    </div>
                </el-image>
            </div>
        </div>

        <CustomDescriptions :data="descriptionsData" :styleBox="{ width: '40%' }"></CustomDescriptions>

        <el-form :inline="true" :model="formInlineSearch" class="form-inline " label-width="80px">
            <el-form-item label="ID">
                <el-input v-model="formInlineSearch.bill_id" placeholder size="small"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="formInlineSearch.status" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" size="small" :label="item.name"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="formInlineSearch.name" placeholder size="small"></el-input>
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="formInlineSearch.phone" placeholder size="small"></el-input>
            </el-form-item>
            <el-form-item label="车牌号">
                <el-input v-model="formInlineSearch.plate" placeholder size="small"></el-input>
            </el-form-item>

            <el-form-item label="选择日期">
                <el-date-picker v-model="changeDate" size="small" type="daterange" range-separator="至"
                    start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            <el-form-item class="btnItem1">
                <el-button type="primary" @click="onSubmit">搜索</el-button>
                <!-- <el-button type="primary" @click="exportHandle">导出</el-button> -->
            </el-form-item>
        </el-form>
        <el-table :data="formDialogTableData.driver.data" tooltip-effect="dark" height="550" style="width: 100%"
            header-align="center" v-loading="tableLoading">
            <el-table-column prop="id" label="运单ID" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">{{ formDialogTableData.id + '-' + scope.row.id }}</template>
            </el-table-column>
            <el-table-column prop="sjxm" label="司机姓名" align="center"></el-table-column>
            <el-table-column prop="phone" label="手机号" align="center" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="cph" label="车牌号" align="center"></el-table-column>
            <el-table-column prop="add_time" label="接单时间" align="center"
                :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="dd_time" label="卸货时间" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span v-if="[4, 5, 6, 7].includes(scope.row.status)">{{ scope.row.dd_time }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="unit_price" label="运费单价" align="center"></el-table-column>
            <el-table-column prop="kg" label="发货重量" align="center"></el-table-column>
            <el-table-column prop="unloading_weight" label="卸货重量" align="center"></el-table-column>
            <el-table-column prop="price" label="运费" align="center"></el-table-column>
            <el-table-column prop="status" label="状态" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.status == '0'">待提货</span>
                    <span v-else-if="scope.row.status == '1'">拒绝</span>
                    <span v-else-if="scope.row.status == '2'">确认</span>
                    <span v-else-if="scope.row.status == '3'">运输中</span>
                    <span v-else-if="scope.row.status == '4'">待审核</span>
                    <span v-else-if="scope.row.status == '5'">待结算</span>
                    <span v-else-if="scope.row.status == '6'">已结算</span>
                    <span v-else-if="scope.row.status == '7'">结算失败</span>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="操作" width="250" align="center">
                <template slot-scope="scope">

                    <el-button type="primary" size="mini" @click="driverInfoHandler(scope.row.user_id)">司机信息</el-button>
                    <el-button type="primary" size="mini" v-if="[3, 4, 5, 6, 7].includes(scope.row.status)"
                        @click="traceId = scope.row.id; checkTrace = true">轨迹</el-button>
                    <el-button type="primary" size="mini" :close-on-click-modal="false"
                        v-if="[4].includes(scope.row.status)" @click="checkWaybillHandle(scope.row)">核对运单</el-button>
                    <el-dropdown trigger="click">
                        <span class="el-dropdown-link">
                            <i class="el-icon-arrow-down el-icon--right"></i>
                        </span>
                        <el-dropdown-menu slot="dropdown" class="in-page-dropdown">
                            <el-dropdown-item><el-button type="primary" size="mini"
                                    @click="contractHandler({ user_id: scope.row.user_id, htsn: scope.row.HTsn })">合同链接</el-button>
                            </el-dropdown-item>
                            <el-dropdown-item>
                                <el-button type="primary" size="mini" @click="exportHandle(scope.row.id)">回单</el-button>
                            </el-dropdown-item>
                            <el-dropdown-item>
                                <el-button type="primary" size="mini" @click="deleteSubBill">删除</el-button>
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page.sync="currentPage" :page-sizes="[10, 20, 30, 50]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>

        <el-dialog title="查看回单" :visible.sync="dialogReceipts" width="60%" :before-close="handleClose" :modal="false">
            <div class="imgbox3">
                <div class="block">
                    <div class="demonstration">发货单：</div>
                    <uploadImgPrev :textType="'fahuo'" @getImageList="getImageList" :imageList="[viewReceiptsImgs.pic]">
                    </uploadImgPrev>
                </div>

                <div class="block">
                    <div class="demonstration">收货单：</div>

                    <uploadImgPrev :textType="'shouhuo'" @getImageList="getImageList"
                        :imageList="[viewReceiptsImgs.shd]">
                    </uploadImgPrev>
                </div>

                <div class="block">
                    <div class="demonstration">卸货照片：</div>
                    <uploadImgPrev :textType="'xiehuo'" @getImageList="getImageList"
                        :imageList="[viewReceiptsImgs.xh_img]">
                    </uploadImgPrev>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogReceipts = false">取 消</el-button>
                <el-button type="primary" @click="updateViewReceiptHandle">保 存</el-button>
            </span>
        </el-dialog>

        <el-dialog title="核对运单" class="checkWaybill" :visible.sync="checkWaybillDialog" width="80%" :modal="false">
            <CheckWaybill v-if="checkWaybillDialog" :billDetail="billDetail"
                @refreshTable="refreshTable(() => { checkWaybillDialog = false })" />
        </el-dialog>

        <el-dialog class="carTrace" title="查看轨迹" :visible.sync="checkTrace" width="70%" :modal="false"
            :before-close="CarTraceHandleClose">
            <CarTrace ref="carTrace" v-if="checkTrace" :id="traceId" />
        </el-dialog>

        <el-dialog title="司机信息" :visible.sync="dialogDriverInfoVisible" :modal="false" width="80%">
            <el-form :inline="true" :model="formInlineDriveInfo" class="form-inline dirver" label-width="130px">
                <el-form-item label="用户ID">
                    <el-input v-model="formInlineDriveInfo.id" placeholder></el-input>
                </el-form-item>
                <el-form-item label="司机姓名">
                    <el-input v-model="formInlineDriveInfo.user_name" placeholder></el-input>
                </el-form-item>
                <el-form-item label="驾驶证号">
                    <el-input v-model="formInlineDriveInfo.jszh" placeholder></el-input>
                </el-form-item>
                <el-form-item label="驾驶证发证机关">
                    <el-input v-model="formInlineDriveInfo.IssueUnit" placeholder></el-input>
                </el-form-item>
                <el-form-item label="驾驶证到期">
                    <el-input v-model="formInlineDriveInfo.driver_end_date"
                        :class="{ 'color-green ': formInlineDriveInfo.driverClass }" placeholder></el-input>
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="formInlineDriveInfo.sfz" placeholder></el-input>
                </el-form-item>
                <el-form-item label="身份证到期">
                    <el-input v-model="formInlineDriveInfo.id_end_date"
                        :class="{ 'color-green ': formInlineDriveInfo.idClass }" placeholder></el-input>
                </el-form-item>

                <div class="driver-box">
                    <div class="driver-item">
                        <p>身份证正面</p>
                        <el-image style="width: 320px; height: 240px" :src="formInlineDriveInfo.sfzimgz"
                            :preview-src-list="[formInlineDriveInfo.sfzimgz]"></el-image>
                    </div>
                    <div class="driver-item">
                        <p>身份证反面</p>
                        <el-image style="width: 320px; height: 240px" :src="formInlineDriveInfo.sfzimgb"
                            :preview-src-list="[formInlineDriveInfo.sfzimgb]"></el-image>
                    </div>

                    <div class="driver-item">
                        <p>驾驶证正面</p>
                        <el-image style="width: 320px; height: 240px" :src="formInlineDriveInfo.jszz"
                            :preview-src-list="[formInlineDriveInfo.jszz]"></el-image>
                    </div>

                    <div class="driver-item">
                        <p>驾驶证附页正面</p>
                        <el-image style="width: 320px; height: 240px" :src="formInlineDriveInfo.jszfz"
                            :preview-src-list="[formInlineDriveInfo.jszfz]"></el-image>
                    </div>

                    <div class="driver-item">
                        <p>驾驶证附页反面</p>
                        <el-image style="width: 320px; height: 240px" :src="formInlineDriveInfo.jszfb"
                            :preview-src-list="[formInlineDriveInfo.jszfb]"></el-image>
                    </div>

                    <div class="driver-item">
                        <p>从业资格证</p>
                        <el-image style="width: 320px; height: 240px" :src="formInlineDriveInfo.cyzgz"
                            :preview-src-list="[formInlineDriveInfo.cyzgz]"></el-image>
                    </div>
                </div>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {
    waybillTracking,
    viewReceipts,
    updateViewReceipts,
    waybillDetails,
    getDriverDetail,
    getContractLink,
    deleteSubBill
} from "@/api/waybill";
import { uploadUrl, getHeader } from "@/utils/tool.js";
import CarTrace from "@/components/CarTrace/CarTrace.vue";
import CheckWaybill from "./checkWaybill.vue";
import uploadImgPrev from "@/components/uploadImgPrev/index.vue";
export default {
    //import引入的组件需要注入到对象中才能使用
    components: {
        CarTrace,
        CheckWaybill,
        uploadImgPrev
    },
    props: ["id"],
    data() {
        //这里存放数据
        return {
            uploadUrl,
            getHeader,
            options: [
                {
                    name: "待提货",
                    value: 1
                },
                {
                    name: "运输中",
                    value: 3
                },
                {
                    name: "待审核",
                    value: 4
                },
                {
                    name: "待结算",
                    value: 5
                },
                {
                    name: "已结算",
                    value: 6
                }
            ],
            formInlineSearch: {
                id: "",
                status: "", //1待提货3运输中4已到达5待结算6已结算
                bill_id: "",
                name: "",
                phone: "",
                plate: "",
                time: "",
                page: 1,
                pageSize: 10
            },
            formInlineSearchQuery: {},
            changeDate: [],
            currentPage: 1,
            total: 0,
            formDialogTableData: {
                hpic2: "",
                hpic: "",
                driver: {
                    data: [],
                }
            },
            dialogReceipts: false,
            viewReceiptsImgs: {
                pic: "",
                shd: "",
                xh_img: "",
                id: ""
            },
            fileList: [],

            checkWaybillDialog: false,
            billDetail: {},
            checkTrace: false,

            tableLoading: true,

            dialogDriverInfoVisible: false,
            formInlineDriveInfo: {
                cyzgz: "",
                jszfb: "",
                jszz: "",
                sfzimgz: "",
                sfzimgb: "",
                jszfz: "",
            },

            descriptionsData: [
                {
                    label: "运单总数",
                    count: 0,
                    unit: "(单)"
                },
                {
                    label: "运费总额",
                    count: 0,
                    unit: "(元)"
                },
            ],
            traceId: '',//查看轨迹需要的id
        };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {
        changeDate(newV, oldV) {
            console.log(newV);
            if (newV && newV.length > 0) {
                this.formInlineSearch.time = newV.join(" - ");
                console.log(this.formInlineSearch.time);
            } else {
                this.formInlineSearch.time = "";
            }
        }
    },
    //方法集合
    methods: {
        async refreshTable(cb) {
            cb && cb();
            await this.waybillTracking({ id: this.id, page: 1, pageSize: 10 });
            this.total = this.formDialogTableData.driver.total;
        },
        getImageList(list, index) {
            console.log(list[0]);
            if (list[0].name == "fahuo") {
                this.viewReceiptsImgs.pic = list[0].url;
            } else if (list[0].name == "shouhuo") {
                this.viewReceiptsImgs.shd = list[0].url;
            } else if (list[0].name == "xiehuo") {
                this.viewReceiptsImgs.xh_img = list[0].url;
            }
        },
        async driverInfoHandler(id) {
            const now = new Date();
            const year = now.getFullYear();
            const month = ("0" + (now.getMonth() + 1)).slice(-2); // 月份从0开始，需要加1；然后通过slice方法补零
            const day = ("0" + now.getDate()).slice(-2); // 获取日期，并通过slice方法补零

            let dateStr = year + month + day;
            this.dialogDriverInfoVisible = true;
            let res = await getDriverDetail({
                id
            });
            if (res.status == 200) {
                this.formInlineDriveInfo = res.res;
                if (Number(res.res.driver_end_date) > Number(dateStr)) {
                    this.formInlineDriveInfo.driver_end_date =
                        this.formInlineDriveInfo.driver_end_date + " (正常)";
                    this.formInlineDriveInfo.driverClass = true;
                }
                if (Number(res.res.id_end_date) > Number(dateStr)) {
                    this.formInlineDriveInfo.id_end_date =
                        this.formInlineDriveInfo.id_end_date + " (正常)";
                    this.formInlineDriveInfo.idClass = true;
                }
            }
        },

        async checkWaybillHandle(row) {
            let result = await waybillDetails({ id: row.id });
            if (result.code == 200) {
                this.billDetail = result.data;
                this.checkWaybillDialog = true;
            }
        },
        async exportHandle(id) {
            let res = await viewReceipts({ id });
            this.viewReceiptsImgs = res.data;
            this.viewReceiptsImgs.id = id;
            this.dialogReceipts = true;
        },
        handleClose() {
            this.dialogReceipts = false;
        },
        async onSubmit() {
            this.formInlineSearch.page = 1;
            this.formInlineSearchQuery = this.formInlineSearch;
            await this.waybillTracking(this.formInlineSearchQuery);
        },
        handleSizeChange(pageSize) {
            this.formInlineSearch.page = 1;
            this.currentPage = 1;
            this.formInlineSearch.pageSize = pageSize;
            this.formInlineSearchQuery = this.formInlineSearch;
            this.waybillTracking(this.formInlineSearchQuery);
        },
        handleCurrentChange(page) {
            this.formInlineSearch.page = page;
            this.formInlineSearchQuery = this.formInlineSearch;
            this.waybillTracking(this.formInlineSearchQuery);
        },
        async waybillTracking(data) {
            this.tableLoading = true;
            let res = await waybillTracking(data);
            if (res.code == 200) {
                this.formDialogTableData = res.data;
                this.total = res.data.driver.total;
                this.descriptionsData[0].count = res.data.driver.total;
                this.descriptionsData[1].count = res.data.moneys;
                this.formInlineSearch.id = res.data.id;
            }
            this.tableLoading = false;
        },
        handleAvatarSuccess(res, file, type) {
            if (res.code == 200) {
                if (type == "fahuo") {
                    this.viewReceiptsImgs.pic = res.data.url;
                } else if (type == "shouhuo") {
                    this.viewReceiptsImgs.shd = res.data.url;
                } else if (type == "xiehuo") {
                    this.viewReceiptsImgs.xh_img = res.data.url;
                }
            }
        },
        beforeAvatarUpload(file) {
            return true;
            const isJPG = file.type === "image/jpeg";
            const isLt2M = file.size / 1024 / 1024 < 10;

            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 10MB!");
            }
            return isJPG && isLt2M;
        },
        async updateViewReceiptHandle() {
            let res = await updateViewReceipts(this.viewReceiptsImgs);
            if (res.code == 200) {
                this.$message.success(res.msg);
                this.dialogReceipts = false;
            }
        },
        async contractHandler(data) {
            let res = await getContractLink(data);
            if (res.code == 200) {
                window.open(res.url, "_blank");
            }
        },
        async deleteSubBill(id) {
            this.$confirm('确定删除该分运单?', '提示', {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(async () => {
                let res = await deleteSubBill({
                    id
                })
                if (res.code == 200) {
                    this.$message.success(res.msg)
                    this.waybillTracking(this.formInlineSearchQuery);
                } else {
                    this.$message.error(res.msg)
                }
            }).catch(() => {

            })

        },
        CarTraceHandleClose() {//CarTrace组件关闭的回调,用地图组件的地方必须要写,要不然会造成内存泄露
            if (this.$refs.carTrace.traceMap) {
                this.$refs.carTrace.traceMarker.stopMove(); //先暂停轨迹动画然后再销毁 否则报错
                this.$refs.carTrace.traceMap.destroy();
            }
            this.checkTrace = false;
        },
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() { },
    //生命周期 - 挂载完成（可以访问DOM元素）
    async mounted() {
        this.refreshTable();
    },
    beforeCreate() { }, //生命周期 - 创建之前
    beforeMount() { }, //生命周期 - 挂载之前
    beforeUpdate() { }, //生命周期 - 更新之前
    updated() { }, //生命周期 - 更新之后
    beforeDestroy() { }, //生命周期 - 销毁之前
    destroyed() { }, //生命周期 - 销毁完成
    activated() { } //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.statistics {
    padding: 10px;
    font-size: 18px;
}

.imgs-box {
    display: flex;
    align-items: center;
    width: 100%;
    height: 260px;
    padding: 0 50px;

    .img-box {
        width: 36%;
        height: 100%;
        display: flex;

        .title {
            width: 20%;
        }
    }

    ::v-deep .image-slot {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        background-color: #f5f7fa;
    }
}


.imgbox3 {
    display: flex;

    .block {
        display: flex;
        padding: 30px;
        width: 30%;
        height: 300px;
        overflow: hidden;

        .demonstration {
            width: 40%;
        }
    }
}

.carTrace {
    ::v-deep .el-dialog {
        height: 80vh;

        .el-dialog__body {
            height: calc(80vh - 55px);
            overflow: hidden;
        }
    }
}

.checkWaybill {
    ::v-deep .el-dialog {
        max-height: 88vh;
        overflow: hidden;

        .el-dialog__body {
            max-height: calc(88vh - 54px);
            overflow: auto;
        }
    }
}

::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

::v-deep .avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

::v-deep .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

::v-deep .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

::v-deep .el-input__inner {
    height: 32px !important;
}


::v-deep .form-inline {
    display: flex;
    flex-wrap: wrap;

    .btnItem1 {
        flex: 1;
        display: flex;
        justify-content: flex-end;

        .el-form-item__content {
            width: auto !important;
        }

        .btn {
            margin-right: 22px;
        }
    }

    .el-form-item__label {
        // min-width: 80%;
        overflow: hidden;
        white-space: nowrap;
    }

    .el-date-editor--daterange {
        width: 100%;

        .el-range-separator {
            width: 10% !important;
        }
    }

    .el-input,
    .el-select {
        width: 100%;
    }
}

.dirver {

    .color-green {
        ::v-deep .el-input__inner {
            color: green;
        }
    }

    ::v-deep .el-form-item {
        width: 25% !important;
        margin-right: 0px;
    }

    .driver-box {
        display: flex;
        flex-wrap: wrap;
        width: 100%;
        justify-content: space-around;

        .driver-item {
            width: 30%;
            display: flex;
            align-items: center;
            flex-direction: column;
            margin: 10px;
        }
    }
}

::v-deep .el-table {
    .el-button {
        padding: 6px;
    }
}
</style>